import api from "./api.js"
import config from "./config.js"

export default {
    isbool(param) {
        // console.log(param)
        if (param == '') {
            return false
        }
        if (param == []) {
            return false
        }
        if (param == "[]") {
            return false
        }
        if (param == undefined) {
            return false
        }
        if (param == {}) {
            return false
        }
        if (param == 0) {
            return false
        }
        if (param == "0") {
            return false
        }
        if (param == "null") {
            return false
        }
        if (param == null) {
            return false
        }
        return true
    },
    isTrue(param){return this.isbool(param)},
    log(info, text) {
        if (text !== undefined) {
            info = text + info
        }
        if (config.loggerStatus == 1) {
            console.log(info);
        }
        if (config.loggerStatus == 2) {
            logHtml(
                "==========================================" + "<br>" + "caller:" + log.caller + "<br>"
                + "--------------------------------------------------------------------------" + "<br>"
                + info + "<br>"
                + "==========================================" + "<br><" + this.caller + "br><br><br>"
                // + "<br><br><br><br>"
            );
        }
    },
    logJson(info, text) {
        info = JSON.stringify(info)
        this.log(info, text)
    },
    getDataFromUrl(url) {
        var queryArr = data.split("?")
        return queryArr[1]
    },
    getDataFromQuery(data) {
        var result = {}
        var queryArr = data.split("&")
        queryArr.forEach(function (item) {
            var value = item.split("=")[1]
            var key = item.split("=")[0]
            result[key] = value
        })
        return result
    },
    getLocData(key) {
        return uni.getStorageSync(key)
    },
    setLocData(key, value) {
        if (typeof value != "string") {
            value = JSON.stringify(value)
        }
        uni.setStorageSync(key, value)
    },
    getLocDataAsync(key,func) {
        uni.getStorage({
            key: key,
            success: function (res) {
                func(res.data)
            }
        });
    },
    setLocDataAsync(key, value) {
        if (typeof value != "string") {
            value = JSON.stringify(value)
        }
        uni.setStorage({
            key: key,
            data: value,
            success: function () {
                console.log('success');
            }
        });
        // uni.setStorageSync(key, value)
    },
    delLocData(key) {
        uni.removeStorageSync(key);
    },
    clearLocData(key) {
        uni.clearStorageSync();
    },


    objDelIndex(obj, index) {
        var tmp = {};
        for (var i in obj) {
            if (i != index) {
                tmp[i] = obj[i]
            }
        }
        return tmp;
    },

    objDelValue(obj, value) {
        var tmp = {};
        for (var i in obj) {
            if (obj[i] != value) {
                tmp[i] = obj[i]
            }
        }
        return tmp;
    },


    arrayDelIndex(array, index) {
        var tmp = [];
        array.map(function (data, i) {
            if (i != index) {
                tmp.push(data)
            }
        })
        return tmp;
    },

    arrayDelValue(array, value) {
        var tmp = [];
        array.map(function (data, i) {
            if (data != value) {
                tmp.push(data)
            }
        })
        return tmp;
    },

    isInArray(val, array) {
        if (!isTrue(array)) {
            return false
        }
        for (var i in array) {
            if (array[i] == val) {
                return true;
            }
        }
        return false;
    },
    arrayReplaceValue(array, find, replace) {
        var tmp = [];
        array.map(function (data, i) {
            if (data != find) {
                tmp.push(data)
            } else {
                tmp.push(replace)
            }
        })
        return tmp;
    },
	showToast(title,duration){
		if (duration == undefined) {
			duration = 2000
		}
		uni.showToast({
			title: title,
			duration: duration
		});
	},
    picUrl(pic){
        if (this.isbool(pic)) {
            return api.getAssetBaseUrl() + pic;
        }
        return ""
    },
    picBg(pic){
        var rnd = Math.floor(Math.random()*10);//0-9
        var color = ["#ffd6d6","#ffead6","#fffed6","#eeffd6","#ddffd6","#d6ffef","#d6f1ff","#dbd6ff","#f8d6ff","#ffd6e3"]
        if (this.isbool(pic)) {
            return "background-size: cover;background-color: "+ color[rnd] +";background-image: url('" + this.picUrl(pic) +"');";
        }else {
            return "background-size: cover;background-color: "+ color[rnd] +";";
        }
    },
    rndbg(){
        var rnd = Math.floor(Math.random()*10);//0-9
        return ""
    }
}